package com.wlyuan.open.scanner.repository.impl;

import com.wlyuan.open.scanner.dao.entity.OrderIndexDO;
import com.wlyuan.open.scanner.domain.quotation.QuotationDetailConvert;
import com.wlyuan.open.scanner.domain.quotation.SurchargeFee;
import com.wlyuan.open.scanner.repository.PriceOrderCacheRepository;
import com.wlyuan.open.scanner.repository.QuotationDetailRepository;
import com.wlyuan.open.scanner.repository.TemplateRepository;
import com.wlyuan.open.scanner.utils.NumberUtils;
import com.wlyuan.open.scanner.utils.enums.YesOrNoEnum;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Optional;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class BaseCostRepository {
    private final PriceOrderCacheRepository priceOrderCacheRepository;
    private final QuotationDetailRepository quotationDetailRepository;
    private final TemplateRepository templateRepository;

    /**
     * 费用
     *
     * @param data
     */
    public void cost(OrderIndexDO data, Long down, boolean waybill) {
        //回执费用
        data.setReceiptFee(NumberUtils.receiptCost(data.getTotalCost(), data.getPayment(), data.getPayable(), data.getPaysub()));
        //异常费用
        data.setExceptionFee(priceOrderCacheRepository.getExceptionCost(data.getIndexId()));
        //是否有报价
        boolean quotation = quotationDetailRepository.getOrderQuotation(LongUtils.notNullLong(down) ? down : data.getIndexId()) &&
                priceOrderCacheRepository.checkQuotation(LongUtils.notNullLong(down) ? down : data.getIndexId());
        data.setQuotation(quotation);
        if (waybill) {
            //是否合约匹配
            data.setMatched(YesOrNoEnum.NO.getValue());
            if (LongUtils.notNullLong(data.getDocId())) {
                var list = templateRepository.getTemplateMatch(data.getDocId());
                if (!CollectionUtils.isEmpty(list)) {
                    data.setTemplateId(data.getDocId());
                    data.setMatched(YesOrNoEnum.YES.getValue());
                }
            }
            //附加费用明细
            data.setSurchargeFees(QuotationDetailConvert.convertOrderCacheSurcharge(priceOrderCacheRepository.getAdditionByOrderId(LongUtils.notNullLong(down) ? down : data.getIndexId())));
            if (!CollectionUtils.isEmpty(data.getSurchargeFees()) && data.getSurchargeFees().stream().map(SurchargeFee::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add).compareTo(NumberUtils.changeZero(data.getAdditionCost())) != 0) {
                var quotationSurchargeFee = quotationDetailRepository.getSurchargeByOrderId(LongUtils.notNullLong(down) ? down : data.getIndexId());
                Optional.ofNullable(quotationSurchargeFee).ifPresent(surcharge -> {
                    data.setQuoteSurchargeFee(surcharge.getQuoteSurchargeFee());
                    data.setSurchargeFees(surcharge.getSurchargeFeeList());
                });
            }
        }
    }
}
